<?php

namespace Common\Logic;

/**
 * Description of 管理员登录业务逻辑层
 *
 * @author songanwei
 * @date 2015-12-31
 */
class AdminLoginLogic extends \Common\Controller\BaseController {

    /**
     * 登陆验证
     * @modify songanwei 2016-01-08
     * @modify paddy 2016-12-27 发生错误时，将错误作为返回值
     * @modify wenhui 2017-06-16 新增手机区号
     * @param string $mobile  手机号
     * @param string $areaCode  手机区号
     * @param string $password 密码
     */
    public function login($mobile, $password, $withJump = true, $areaCode = '86') {
        $adminLoginModel = D('AdminLogin');
        $adminLogin = $adminLoginModel->getInfoByMobile($mobile, $areaCode);
        if (empty($adminLogin)) {
            if($withJump){
                returnJson(403, 100);
            }else {
                return 100;
            }
        }
        if ($adminLogin['password'] != $password) {
            if($withJump){
                returnJson(403, 101);
            }else {
                return 101;
            }
        }
        //查询
        $adminInfoModel = D('AdminInfo');
        $adminInfo = $adminInfoModel->getInfoByUser($adminLogin['id']);
        unset($adminInfo['id']);
        unset($adminInfo['adminlogin_id']);
        //组合数据
        return array_merge($adminLogin, $adminInfo);
    }

    public function getInfo($userId, $hotel = null) {
        return $this->__singleGet('_getInfo', array($userId, $hotel));
    }

    /**
     * 返回用户数据
     * @modify songanwei 2016-01-08
     * @modify trainyao 2016-01-20    增加查询多个情况
     * @modify trainyao 2016-03-23      增加获取用户身份数据
     * @param integer/array $userId 用户ID
     * @return type
     */
    protected function _getInfo($userId, $hotel) {
        $adminLoginModel = D('AdminLogin');
        $adminLogin = $adminLoginModel->getInfo($userId);
        if (!$adminLogin) {
            return array();
        }
        $adminInfoModel = D('AdminInfo');
        $adminInfo = $adminInfoModel->getInfoByUser($userId);
        // 分情况:查询一个或多个时
        if ((is_array($userId) && count($userId) == 1) || (!is_array($userId) && strpos($userId, ',') === false)) {
            unset($adminInfo['id']);
            unset($adminInfo['adminlogin_id']);

            // 获取用户角色
            $adminHotelModel = D('AdminHotel');
            if (is_null($hotel)) {
                $adminHotels = $adminHotelModel->getInfosByAdmin($adminLogin['id']);
                $adminLogin['role'] = $adminHotels ? 1 : 2;
            } else {
                $adminHotels = $adminHotelModel->getInfoByAdminHotel($adminLogin['id'], $hotel);
                $adminLogin['role'] = $adminHotels['role'];
            }

            //组合数据
            return array_merge($adminLogin, $adminInfo);
        } else {
            $adminInfo = getArrayValueAsIndex($adminInfo, 'adminlogin_id');
            // 获取用户们的角色
            $adminHotelModel = D('AdminHotel');
            $adminHotels = $adminHotelModel->getInfosByAdmin($userId);
            $adminHotels = getArrayValueAsIndex($adminHotels, 'adminlogin_id', 'multiStruct');

            foreach ($adminLogin as &$adminLoginItem) {
                $tmpUserId = $adminLoginItem['id'];
                unset($adminInfo[$tmpUserId]['id']);
                unset($adminInfo[$tmpUserId]['adminlogin_id']);
                $adminLoginItem = array_merge($adminLoginItem, $adminInfo[$tmpUserId]);
                $adminLoginItem['role'] = $adminHotels[$tmpUserId] ? 1 : 2;
            }
            unset($adminLoginItem);

            return $adminLogin;
        }
    }

    /**
     * 更新用户数据
     * @modfiy songanwei 2016-01-08
     * @param integer $userId  用户ID
     * @param string $name  用户名称
     * @param string $headpath  用户头像
     */
    public function saveInfo($userId, $name, $headpath) {
        $adminLoginModel = D('AdminLogin');
        //检查数据
        $adminLogin = $adminLoginModel->getInfo($userId);
        if (!$adminLogin) {
            returnJson(500, 501);
        }

        $adminLoginModel->startTrans();
        $commit = true;
        $result = $adminLoginModel->setName($userId, $name);
        if ($result == false) {
            $commit = false;
        }
        $adminInfoModel = D('AdminInfo');
        if ($commit) {
            $result = $adminInfoModel->saveInfo($userId, $headpath);
            if ($result == false) {
                $commit = false;
            }
        }
        if ($commit) {
            $adminInfoModel->commit();
        } else {
            $adminLoginModel->rollback();
            returnJson(500, 509);
        }
    }

    /**
     * 用户登录检查token
     * @modify songanwei 2016-01-22
     * @param integer $userId  用户id
     * @param string $token 用户Token
     * @return boolean
     */
    public function checkToken($userId, $token) {
        // rsa解密和加密
        $RsaEncrypt = new \Org\Util\RSAEncrypt();
        $decryptPassword = $RsaEncrypt->meizhuDecrypt($token);
        if (!$decryptPassword) {
            return false;
        }
        //获取盐
        $salt = substr($decryptPassword, 0, 14);
        //获取token密码
        $password = substr($decryptPassword, 14);
        // $current = strtotime(date('YmdHis'));

        $adminLoginModel = D('AdminLogin');
        $adminLogin = $adminLoginModel->getInfo($userId);

        //检查密码是否正确
        if (hash('sha256', $password) != $adminLogin['password']) {
            return false;
        }
        return true;
    }

}
